/*
 * Copyright (c) 2014, Asiainfo-Linkage. All rights reserved.
 * Asiainfo-Linkage PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package edu.w106.platform.mc;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

/**
 * 展现系统托盘消息.
 * @author w106
 *
 */
public class TrayMessage extends MessageCenter {

    private static TrayMessage tray = null;
    private TrayIcon trayIcon;

    private final Image defaultImg;
    private final String defaultTitle = "消息中心";

    private TrayMessage() {

        URL url = this.getClass().getClassLoader().getResource("config/icon.png");

        defaultImg = Toolkit.getDefaultToolkit().getImage(url);

        init();
    }

    /*    private String getTop5Message() {
            StringBuffer buf = new StringBuffer();
            buf.append("----------消息明细----------");
            for (int i = 0; i < 5 && !receMsg.isEmpty(); i++) {
                Message m = receMsg.poll();
                buf.append("\r\n消息类型:" + m.getTypeName() + m.getContent());
            }
            return buf.toString();
        }*/

    public static TrayMessage getInstance() {
        if (tray == null) {
            tray = new TrayMessage();
        }
        return tray;
    }

    /*    private String getUnReadMsgTitle() {
            return "未读消息数量:" + receMsg.size();
        }*/

    /*    @Override
        public void notifyMessage() {
            if (receMsg.isEmpty()) {
                trayIcon.setToolTip(defaultTitle);
            } else {
                trayIcon.setToolTip(getUnReadMsgTitle());
            }
        }*/

    private void init() {
        trayIcon = new TrayIcon(defaultImg, defaultTitle);
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                trayIcon.displayMessage("退出", "程序即将退出", TrayIcon.MessageType.WARNING);
                sleepThread(3000);
                System.exit(0);
            }
        });

        PopupMenu popup = new PopupMenu();
        popup.add(exitItem);

        trayIcon.setPopupMenu(popup);
        trayIcon.setImageAutoSize(true);

        trayIcon.addMouseListener(new TrayMouseAdapter());

        SystemTray tray = SystemTray.getSystemTray();
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("TrayIcon could not be added.");
        }

        new TrayThread().start();

    }

    private void sleepThread(int s) {
        try {
            Thread.sleep(s);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 鼠标处理事件.
     * @author w106
     *
     */
    private class TrayMouseAdapter extends MouseAdapter {
        @Override
        public void mouseEntered(MouseEvent e) {
            if (receMsg.isEmpty()) {
                trayIcon.setToolTip(defaultTitle);
            } else {
                trayIcon.setToolTip("未读消息数量:" + receMsg.size());
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {

            if (e.getClickCount() == 2 && !receMsg.isEmpty()) {
                Message m = receMsg.poll();
                if (m.getType() == Message.MSG_ERROR) {
                    trayIcon.displayMessage("错误", m.getContent(), TrayIcon.MessageType.ERROR);
                } else if (m.getType() == Message.MSG_INFO) {
                    trayIcon.displayMessage("消息", m.getContent(), TrayIcon.MessageType.INFO);
                } else {
                    trayIcon.displayMessage("消息", m.getContent(), TrayIcon.MessageType.WARNING);
                }
            }

        }
    }

    /**
     * 如果有消息。则闪烁图标.
     * @author w106
     *
     */
    private class TrayThread extends Thread {
        @Override
        public void run() {
            while (true) {
                if (receMsg.isEmpty()) {
                    trayIcon.setImage(defaultImg);
                    sleepThread(1000);
                } else {
                    sleepThread(500);
                    trayIcon.setImage(Toolkit.getDefaultToolkit().createImage(""));
                    sleepThread(500);
                    trayIcon.setImage(defaultImg);
                }

            }
        }
    }
}
